let app = new Vue({
    el: '#app',
    data: {
        admin: JSON.parse(sessionStorage.getItem("admin")),
        difficult: 1,
    },
    created() {

        layui.use('form', function () {

            $.ajax({
                url: 'http://localhost:8080/subject/selectLevelNameByDistinct',
                type: 'get',
                success: function (vo) {
                    if (vo.code == 200) {
                        $.each(vo.data, function (i, s) {
                            $("#levelName").append("<option value='" + s.levelName + "'>" + s.levelName + "</option>");
                        });
                    }
                    layui.form.render();
                }
            });

        });



    },
    methods: {
        save: function () {
            // 非空判断
            if ($("#title").val() == '' || $("#resolution").val() == '' || $("#levelName").val() == '' || $("#subjectName").val() == '' || $("#questionA").val() == '' || $("#questionB").val() == '' || $("#questionC").val() == '' || $("#questionD").val() == '' || $("#questionE").val() == '' || $("input:checkbox[name='correctAnswer']:checked").val() == null || $("#score").val() == '') {
                layer.msg("必填字段不能为空", { offset: 't', icon: 5 });
                return;
            }

            // 循环获取选中的复选框的内容并拼接
            let correctAnswer = '';
            $("input:checkbox[name='correctAnswer']:checked").each(function () {
                if($(this).val()=='a'){
                    correctAnswer = correctAnswer + $("#questionA").val() + "&";
                }else if($(this).val()=='b'){
                    correctAnswer = correctAnswer + $("#questionB").val() + "&";
                }else if($(this).val()=='c'){
                    correctAnswer = correctAnswer + $("#questionC").val() + "&";
                }else if($(this).val()=='d'){
                    correctAnswer = correctAnswer + $("#questionD").val() + "&";
                }else if($(this).val()=='e'){
                    correctAnswer = correctAnswer + $("#questionE").val() + "&";
                }
            });
            //去掉最后一个"&"逗号
            if (correctAnswer.length > 0) {
                correctAnswer = correctAnswer.substr(0, correctAnswer.length - 1);
            }

            $.ajax({
                url: 'http://localhost:8080/questionContent/add',
                type: 'post',
                data: { title: $("#title").val(), questionA: $("#questionA").val(), questionB: $("#questionB").val(), questionC: $("#questionC").val(), questionD: $("#questionD").val(),questionE: $("#questionE").val(), resolution: $("#resolution").val() },
                success: function (vo) {
                    if (vo.code == 200) {

                        let formData = {
                            questionType: 2,
                            subjectName: $("#subjectName").val(),
                            levelName: $("#levelName").val(),
                            score: $("#score").val(),
                            difficult: app.difficult,
                            correctAnswer: correctAnswer,
                            questionContentId: vo.data,
                            createUser: app.admin.userId
                        }
                        $.ajax({
                            url: 'http://localhost:8080/paperQuestion/add',
                            type: 'post',
                            data: formData,
                            success: function (vo2) {
                                if (vo2.code == 200) {
                                    
                                    layer.msg(vo2.data, { offset: 't', icon: 1 });
                                    // 延迟1秒执行代码
                                    setTimeout(function () {
                                        // 重置表单
                                        $('#app')[0].reset();
                                        layui.form.render();
                                    }, 1000);

                                } else {
                                    layer.msg(vo2.message, { offset: 't', icon: 2 });
                                }
                            }
                        });

                    }
                }
            });
        },
        test: function () {
            $("#title2").text($("#title").val());
            $("#questionA2").text($("#questionA").val());
            $("#questionB2").text($("#questionB").val());
            $("#questionC2").text($("#questionC").val());
            $("#questionD2").text($("#questionD").val());
            $("#questionE2").text($("#questionE").val());
            layer.open({
                type: 1,
                title: ['预览', 'font-size:16px'],
                anim: 5,   // 动画
                offset: '50px',  // 坐标
                resize: false,  // 是否允许拉伸改变宽高
                area: ['auto'], // 设置宽，高
                shadeClose: true, //点击遮罩关闭
                scrollbar: false,   // 屏蔽浏览器滚动（false: 则屏蔽）
                content: $("#show-test")
            });
        }
    }
});